#include "widget.h"

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    dock = new Dock(this);
    musicTable = new MusicTable(this);
    controlBar = new ControlBar(this);
    networkCommunicator = new NetworkCommunicator(this);

    QStringList playlists = Database::get()->getPlaylists();
    for(QList<QString>::iterator itr = playlists.begin(); itr != playlists.end(); itr++)
    {
        QAction* act = new QAction((*itr), musicTable);
        QObject::connect(act, SIGNAL(triggered()), this, SLOT(addSongToPlaylist()));
        musicTable->addToPlaylistMenu(act);
        dock->addPlaylist(*itr);
    }

    QHBoxLayout* l = new QHBoxLayout;
    l->setContentsMargins(0, 0, 0, 0);
    l->setSizeConstraint(QLayout::SetMaximumSize);
    l->addWidget(dock);
    l->addWidget(musicTable);

    QVBoxLayout* rightPaneLayout = new QVBoxLayout(this);
    rightPaneLayout->addLayout(l);
    rightPaneLayout->addWidget(controlBar, 0, Qt::AlignBottom);

    setLayout(rightPaneLayout);

    QObject::connect(controlBar, SIGNAL(nextSong()), this, SLOT(nextSong()));
    QObject::connect(controlBar, SIGNAL(previousSong()), this, SLOT(previousSong()));

    QObject::connect(networkCommunicator, SIGNAL(newStreamUrlFound(QUrl)), this, SLOT(playSong(QUrl)));
    QObject::connect(networkCommunicator, SIGNAL(newSearchDone(Playlist)), musicTable, SLOT(loadPlaylist(Playlist)));
    QObject::connect(musicTable, SIGNAL(deleteSongFromPlaylist(VideoInfo)), Database::get(), SLOT(deleteSongFromPlaylist(VideoInfo)));
    QObject::connect(musicTable, SIGNAL(songClicked(VideoInfo)), this, SLOT(requestStreamUrl(VideoInfo)));
    QObject::connect(musicTable, SIGNAL(editClicked()), this, SLOT(editSong()));
    QObject::connect(dock, SIGNAL(playlistClicked(QString)), this, SLOT(loadPlaylist(QString)));
    QObject::connect(dock, SIGNAL(searchRequested(QString)), this, SLOT(performSearch(QString)));
}

void Widget::addNewPlaylist(QString name)
{
    QAction* act = new QAction(name, musicTable);
    QObject::connect(act, SIGNAL(triggered()), this, SLOT(addSongToPlaylist()));
    musicTable->addToPlaylistMenu(act);
    dock->addPlaylist(name);
    Database::get()->addNewPlaylist(name);
}

void Widget::loadPlaylist(QString name)
{
    qDebug() << "loading " << name;
    if(showingPlaylist.getName() != name)
    {
        showingPlaylist = name;
        qDebug() << "about to reveive videos";

        Playlist videos = Database::get()->getPlaylist(name);
        musicTable->loadPlaylist(videos);
    }
}

void Widget::performSearch(QString tags)
{
    showingPlaylist = Playlist();
    networkCommunicator->search(tags);
}

void Widget::requestStreamUrl(VideoInfo vid)
{
    currentVideo = vid;
    networkCommunicator->findStreamUrl(vid);
}

void Widget::playSong(QUrl url)
{
    controlBar->initNewSong(currentVideo);
    VideoPlayer::get()->playSong(url);
}

void Widget::addSongToPlaylist()
{
    QAction* act = qobject_cast<QAction*>(sender());
    VideoInfo vid = musicTable->getHighlightedVideo();

    if(vid.playlist != act->text())
    {
        qDebug() << vid.playlist;
        qDebug() << act->text();
        Database::get()->addNewSong(act->text(), vid);
    }
}

void Widget::nextSong()
{
    QList<VideoInfo> vids = playingPlaylist.getVids();
    int i = vids.indexOf(currentVideo);
    if(i > 0 && i+1 != vids.length())
    {
        currentVideo = vids.at(i+1);
        networkCommunicator->findStreamUrl(currentVideo);
    }
}

void Widget::previousSong()
{

}

void Widget::editSong()
{
    //Get the song's current properties
    VideoInfo vid = musicTable->getHighlightedVideo();
    QInputDialog dialog;
    dialog.setLabelText("Title: ");
    dialog.setInputMode(QInputDialog::TextInput);
    dialog.setTextValue(vid.title);

    if(dialog.exec())
    {
        Database::get()->changeVideoAttribute(showingPlaylist.getName(), vid.id, "title", dialog.textValue());
        loadPlaylist(showingPlaylist.getName());
    }
}
